package ar.uba.dc.tl.mylanga.inputhandler;

import java.io.PushbackReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class InputHandler {

	protected static final int PUSH_BACK_SIZE = 1024;

	private static final Map<Input, InputHandler> handlers = new HashMap<Input, InputHandler>();
	private List<String> arguments;

	public InputHandler(Input inputSource) {
		this(inputSource, null);
	}

	public InputHandler(Input inputSource, List<String> arguments) {
		assert inputSource != null;

		if (arguments != null) {
			this.setArguments(arguments);
		}

		InputHandler.registerHandlerFor(inputSource, this);
	}

	public static InputHandler handlerFor(Input inputSource) {
		assert inputSource != null;

		InputHandler inputSourceHandler = InputHandler.handlers.get(inputSource);

		if (inputSourceHandler == null) {
			throw new RuntimeException("unknown input source");
		}

		return inputSourceHandler;
	}

	public static void registerHandlerFor(Input inputSource, InputHandler inputSourceHandler) {
		assert inputSource != null;
		assert inputSourceHandler != null;

		InputHandler.handlers.put(inputSource, inputSourceHandler);
	}

	public abstract void checkArguments();

	public List<String> getArguments() {
		return this.arguments;
	}

	public void setArguments(List<String> arguments) {
		assert arguments != null;

		this.arguments = new ArrayList<String>(arguments);
	}

	public abstract PushbackReader getReader();
}
